package cn.com.dcsgo.library.client.domain.vo;

import cn.com.dcsgo.library.client.domain.po.BorrowRecord;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dcsgo
 * @since 2024/9/6 08:24
 */
@Data
@NoArgsConstructor
@ApiModel(description = "借阅记录VO")
public class BorrowRecordVO {
    @ApiModelProperty("复本编号")
    private Long copyId;

    @ApiModelProperty("图书名")
    private String title;

    @ApiModelProperty("借出日期")
    @JsonFormat(pattern = "yyyy年M月d日 H:m")
    private LocalDateTime borrowData;

    @ApiModelProperty("应还日期")
    @JsonFormat(pattern = "yyyy年M月d日 H:m")
    private LocalDateTime dueDate;

    @ApiModelProperty("借阅天数")
    private Long borrowDays;

    public static List<BorrowRecordVO> of(List<BorrowRecord> borrowRecords) {
        ArrayList<BorrowRecordVO> vos = new ArrayList<>(borrowRecords.size());
        for (BorrowRecord borrowRecord : borrowRecords) {
            BorrowRecordVO borrowRecordVO = new BorrowRecordVO();
            borrowRecordVO.setCopyId(borrowRecord.getBookCopiesId());
            borrowRecordVO.setBorrowData(borrowRecord.getBorrowDate());
            borrowRecordVO.setDueDate(borrowRecord.getDueDate());
            borrowRecordVO.setBorrowDays(LocalDateTimeUtil.between(borrowRecord.getBorrowDate(), LocalDateTime.now()).toDays());
            vos.add(borrowRecordVO);
        }
        return vos;
    }
}
